#include <stl/set.h>
#include <standard/math.h>
#include "sceneobjectaddcmd.h"
#include "editorscenemgr.h"
#include "sceneobjects/sceneobject.h"
#include "sceneobjects/sceneobjectmanager.h"
#include "scene/sceneobjectnavigationmap.h"


CSceneObjectAddCmd::CSceneObjectAddCmd( const char* strName, const char* strFileName, float fInitScale, float fHelperRadius, int32 nCellType, int32 nRotationType, ESceneObjectType eSceneObjectType, const char* strExtData, float x, float y, float z, int32 nGridX, int32 nGridY, int32 nBrushSize, int32 nBrushType){
    strncpy( m_strName, strName, 32);
    strncpy( m_strFileName, strFileName, 32);
    m_fInitScale = fInitScale;
    m_fHelperScale = fHelperRadius;
    m_nCellType = nCellType;
    m_nRotationType = nRotationType;
    m_eSceneObjectType = eSceneObjectType;
    m_nBrushSize = nBrushSize;
    m_nBrushType = nBrushType;
    m_fX = x;
    m_fY = y;
    m_fZ = z;
    m_nGridX = nGridX;
    m_nGridY = nGridY;
    m_strExtData = strExtData;
}
CSceneObjectAddCmd::~CSceneObjectAddCmd(){

}
void CSceneObjectAddCmd::Init(){
    CSceneObjectNavigationMap* _pSceneObjectNavigationMap = m_pSceneMgr->GetSceneObjectNavigationMap();
    Vector3 _vPosition = { m_fX, m_fY, m_fZ };
    Vector3 _vRotation = { 0.0f, 0.0f, 0.0f };
    Vector3 _vScale = { m_fInitScale, m_fInitScale, m_fInitScale };
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    CSceneObject* _pSceneObject = _pSceneObjectManager->AddSceneObject( 0u, m_strFileName, m_strName, m_eSceneObjectType, m_fInitScale, m_fHelperScale, m_nCellType, m_nRotationType, m_strExtData.c_str(), _vPosition, _vRotation, _vScale, m_nGridX, m_nGridY );
    m_nObjectID = _pSceneObject->m_nID;
    list<TPosition >_vNavigPositionList;
    _pSceneObjectNavigationMap->GetCellTypeListValue( m_nCellType, 0, m_nGridX, m_nGridY, &_vNavigPositionList );
    _pSceneObjectNavigationMap->SetSOListValue( &_vNavigPositionList, _pSceneObject );
}
void CSceneObjectAddCmd::Final(){

}
bool32 CSceneObjectAddCmd::Redo(){
    CSceneObjectNavigationMap* _pSceneObjectNavigationMap = m_pSceneMgr->GetSceneObjectNavigationMap();
    Vector3 _vPosition = { m_fX, m_fY, m_fZ };
    Vector3 _vRotation = { 0.0f, 0.0f, 0.0f };
    Vector3 _vScale = { m_fInitScale, m_fInitScale, m_fInitScale };
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    CSceneObject* _pSceneObject = _pSceneObjectManager->AddSceneObject( m_nObjectID, m_strFileName, m_strName, m_eSceneObjectType, m_fInitScale, m_fHelperScale, m_nCellType, m_nRotationType, m_strExtData.c_str(), _vPosition, _vRotation, _vScale, m_nGridX, m_nGridY );
    m_nObjectID = _pSceneObject->m_nID;


    list<TPosition >_vNavigPositionList;
    _pSceneObjectNavigationMap->GetCellTypeListValue( m_nCellType, 0, m_nGridX, m_nGridY, &_vNavigPositionList );
    _pSceneObjectNavigationMap->SetSOListValue( &_vNavigPositionList, _pSceneObject );
    return True;
}
bool32 CSceneObjectAddCmd::Undo(){
    CSceneObjectManager* _pSceneObjectManager = m_pSceneMgr->GetSceneObjectMgr();
    CSceneObject* _pSceneObject = _pSceneObjectManager->GetSceneObject( m_nObjectID );
    if( _pSceneObject ){
        CSceneObjectNavigationMap* _pSceneObjectNavigationMap = m_pSceneMgr->GetSceneObjectNavigationMap();
        list<TPosition> _vPathList;
        _pSceneObjectNavigationMap->GetCellTypeListValue( m_nCellType, 0, _pSceneObject->m_sMapGridPos.X, _pSceneObject->m_sMapGridPos.Y, &_vPathList );
        _pSceneObjectNavigationMap->SetSOListValue( &_vPathList, NULL );
        _pSceneObjectManager->RemoveSceneObject( m_nObjectID );
    }
    return True;
}
